package com.fivefu.core.camera.generate;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
import com.fivefu.base.web.controller.BaseController;

import java.util.Scanner;

/**
 * 自动生成代码
 * @author DELL
 *
 */
//https://mp.baomidou.com/config/generator-config.html#strategy
public class MyBatisCodeGenerator {

	/**
	 * <p>
	 * 读取控制台内容
	 * </p>
	 */
	public static String scanner(String tip) {
		Scanner scanner = new Scanner(System.in);
		StringBuilder help = new StringBuilder();
		help.append("请输入" + tip + "：");
		if (scanner.hasNext()) {
			String ipt = scanner.next();
			if (StringUtils.isNotEmpty(ipt)) {
				return ipt;
			}
		}
		throw new MybatisPlusException("请输入正确的" + tip + "！");
	}

	public static void main(String[] args) {
//		String projectPath =  System.getProperty("user.dir")+"/suzhou_wujiang_camera";
		String projectPath = "E://code";
//		String projectPath = "D:\\java测试目录";

		// 数据源配置
		String dbUrl = "jdbc:mysql://192.168.0.9:3306/ffskyeye?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
		DataSourceConfig.Builder dataSourceConfig = new DataSourceConfig.Builder(dbUrl,"shm","shm")
				.dbQuery(new MySqlQuery())
				.typeConvert(new MySqlTypeConvert())
				.keyWordsHandler(new MySqlKeyWordsHandler());

		// 代码生成器
		FastAutoGenerator mpg = FastAutoGenerator.create(dataSourceConfig);
		mpg.globalConfig(globalBuilder -> globalBuilder.fileOverride().disableOpenDir()
				.outputDir(projectPath + "/src/main/java")
				.author("")
				.commentDate("yyyy-MM-dd HH:mm:ss")
				.dateType(DateType.TIME_PACK)
				.enableSwagger());
		mpg.packageConfig(packageBuilder -> packageBuilder
				.parent("com.fivefu.core.camera")
				.service("services")
				.serviceImpl("services.impl")
				.xml("mybatis/master")
		);



		mpg.strategyConfig(strategyconfigBuilder -> strategyconfigBuilder
				.enableCapitalMode()
				.enableSkipView ()
				.disableSqlFilter()
				.addInclude(scanner("表名"))
		);



		//entity 生成策略
		mpg.strategyConfig(strategyconfigBuilder -> strategyconfigBuilder.entityBuilder()
//								.enableTableFieldAnnotation ()
								.naming (NamingStrategy.underline_to_camel)
								.columnNaming (NamingStrategy.underline_to_camel)
								.idType(IdType.AUTO)
//								.enableLombok ()
								/*.logicDeleteColumnName ( "deleted ").logicDeletePropertyName ( "deleted ")
								.addTableFills(new Column( "create_time" , FieldFill.INSERT))
								.addTableFills(new Property( "updateTime " ,FieldFill.INSERT_UPDATE))*/
								.versionColumnName ("version")
								.disableSerialVersionUID()
		);

		//controller 生成策略
		mpg.strategyConfig(
				strategyconfigBuilder ->strategyconfigBuilder
						.controllerBuilder()
						.enableRestStyle()
						.enableHyphenStyle()
						.superClass(BaseController.class)
		);


		//service 生成策略
		mpg.strategyConfig(
				strategyconfigBuilder -> strategyconfigBuilder
						.serviceBuilder()
						.formatServiceFileName ("%sService")
						.formatServiceImplFileName ("%sServiceImpl"));

		//mapper 生成策略
		mpg.strategyConfig (
				strategyconfigBuilder ->strategyconfigBuilder.mapperBuilder()
						.formatMapperFileName ("%sMapper")
						.formatXmlFileName ( "%sMapper" )
						.enableBaseResultMap());

		mpg.execute();
	}

}